package com.ce.pms.common.dao.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.dao.IOrganizationalStructureDao;
import com.ce.pms.common.model.HmDeptDomain;
import com.ce.pms.common.model.HmEmployeeDomain;
import com.ce.pms.common.vo.OrganizationalStructureVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述: OrganizationalStructureDaoImpl 组织架构DAO的实现类
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 许心炜 Created on： 2008-10-7
 * @author 许心炜 update on：  2008-10-29
 */
public class OrganizationalStructureDaoImpl extends PmsBaseDaoImpl<HmEmployeeDomain> implements IOrganizationalStructureDao{

	/**
	 * 功能描述: 根据部门ID查询部门名字 <br>
	 * @param deptId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String getDeptNameById(Long deptId){
		String hql = "from HmDeptDomain hd where hd.id=? ";
		Query query = super.createQuery(hql, deptId);
		Object obj = query.uniqueResult();
		String name = obj==null?null:((HmDeptDomain)obj).getDeptName();
		return name;
	}

	/**
	 * 功能描述: 根据部门ID查询部门实体 <br>
	 * @param deptId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public HmDeptDomain getHmDeptDomainById(Long deptId){
		String hql = "from HmDeptDomain hd where hd.id=? ";
		Query query = super.createQuery(hql, deptId);
		Object obj = query.uniqueResult();
		HmDeptDomain name = obj==null?null:((HmDeptDomain)obj);
		return name;
	}
	/**
	 * 功能描述: 通过员工ID获取员工DOMAIN<br>
	 * @param employeeId
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2009-1-8  <br>
	 */
	public HmEmployeeDomain getHmEmployeeDomainById(Long employeeId){
		StringBuffer queryStr = new StringBuffer(" FROM HmEmployeeDomain EM ");
								queryStr.append(" WHERE 1=1 ");
								queryStr.append(" AND EM.employeeId=? ");
		Query query = super.createQuery(queryStr.toString(), employeeId);
		return (HmEmployeeDomain) query.uniqueResult();
	}
	/**
	 * 功能描述:  查询员工信息列表
	 * @param organizationalStructureVO
	 * @return
	 * Create author:许心炜
	 * Create on:2008-10-7
	 */
	public Page queryOutUserInfo(OrganizationalStructureVO organizationalStructureVO) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String,Object>();

		StringBuffer queryStr = new StringBuffer( "FROM HmEmployeeDomain EM WHERE 1=1 ");//FROM HmEmployeeDomain em JOIN em.deptDO de WHERE 1=1  JOIN USER.HmEmployeeDomain.deptDO  DEPT

		String conditionStr = buildQuerCondition(organizationalStructureVO,paramMap);
		queryStr.append(conditionStr);

		return super.pagedNameQueryByPageNo(queryStr.toString(), organizationalStructureVO.getPageNo(),
				organizationalStructureVO.getPageSize(), paramMap);
	}

	/**
	 * 功能描述:  根据userCode查询员工信息
	 * @param userCode
	 * @return
	 * Create author:许心炜
	 * Create on:2008-10-29
	 */
	public HmEmployeeDomain queryUserByUserCode(String userCode){
		String queryStr = "FROM HmEmployeeDomain EM WHERE EM.sesUser.userCode=? ";

		Query query=createQuery(queryStr, userCode);

		return (HmEmployeeDomain)query.uniqueResult();

	}

	/**
	 * 构建查询条件
	 */
	private String buildQuerCondition(OrganizationalStructureVO organizationalStructureVO,Map<String,Object> paramMap){
		if(organizationalStructureVO == null) return null;
		StringBuffer strBuf = new StringBuffer();

		if(!BlankUtil.isBlank(organizationalStructureVO.getDept())){
			strBuf.append(" AND EM.deptDO.deptName like :dept ");
			paramMap.put("dept", "%" + organizationalStructureVO.getDept() + "%");
		}

		if(!BlankUtil.isBlank(organizationalStructureVO.getEmployeeName())){
			strBuf.append(" AND EM.employeeName like :employeeName ");
			paramMap.put("employeeName", "%" + organizationalStructureVO.getEmployeeName() + "%");
		}

		strBuf.append(" AND EM.employeeName != 'superuser' ");
		return strBuf.toString();
	}

	/**
	 * 功能描述:  根据userID查询部门信息
	 * @param userCode
	 * @return
	 * Create author:许心炜
	 * Create on:2008-12-23
	 */
	public HmDeptDomain queryDeptInfoByUseID(Long userID){
		if(BlankUtil.isBlank(userID)){
			return null;
		}

		String queryStr = "SELECT EM.deptDO FROM HmEmployeeDomain EM WHERE EM.sesUser.userId = ? ";

		Query query=createQuery(queryStr, userID);

		return (HmDeptDomain) query.uniqueResult();
	}
	/**
	 * 功能描述:  根据部门ID查询部门实体
	 * @param userCode
	 * @return
	 * Create author:胡煦
	 * Create on:2008-12-23
	 */
	public HmDeptDomain queryDeptDoById(Long depId){
		if(BlankUtil.isBlank(depId)){
			return null;
		}

		String queryStr = "SELECT EM.deptDO FROM HmEmployeeDomain EM WHERE EM.deptDO.id = ? ";

		Query query=createQuery(queryStr, depId);

		return query.list()==null?null:(HmDeptDomain)query.list().get(0);
	}
	/**
	 * 功能描述:  根据userID查询员工信息<br>
	 * @param userID
	 * @return HmEmployeeDomain
	 * Create author:许心炜 <br>
	 * Create on:2008-12-24  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public HmEmployeeDomain queryUserByUserId(Long userID) {
		// TODO Auto-generated method stub
		String queryStr = "FROM HmEmployeeDomain EM WHERE EM.sesUser.userId = ? ";

		Query query=createQuery(queryStr, userID);

		return (HmEmployeeDomain)query.uniqueResult();
	}
	/**
	 * 功能描述:给员工ID列表,查询所有的员工<br>
	 * @param employeeIds
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-29  <br>
	 */
	public List<HmEmployeeDomain> queryEmployeeByIDs(List<Long> employeeIds){
		String queryStr = "FROM HmEmployeeDomain EM WHERE EM.employeeId IN(:ids) ";
		Query query=createQuery(queryStr);
		query.setParameterList("ids", employeeIds);
		return query.list();
	}

	/**
	 * 功能描述:获得所有部门员工的信息<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-30<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<HmEmployeeDomain> getAllPerson(){

		String queryStr = " FROM HmEmployeeDomain EM ";

		Query query=createQuery(queryStr);
		List<HmEmployeeDomain> list=query.list();
		return list;
	}

	public List<HmDeptDomain> getAllDept(){
		String queryStr = " FROM HmDeptDomain EM";

		Query query=createQuery(queryStr);
		List<HmDeptDomain> list=query.list();
		return list;
	}
}
